package com.copm.ifm.servers.sys.util;

import com.alibaba.fastjson.JSON;
import com.copm.ifm.base.api.util.DictFieldFillUtils;
import com.copm.ifm.base.basic.pojo.DictContentBO;
import com.copm.ifm.base.basic.pojo.DictContentList;
import com.copm.ifm.base.basic.pojo.vo.SysStaffAllVO;
import com.copm.ifm.base.basic.util.AuthUtils;
import com.copm.ifm.common.redis.RedisKeyHelper;
import com.copm.ifm.common.redis.RedisUtil;
import com.copm.ifm.servers.sys.entity.SysDict;
import com.copm.ifm.servers.sys.entity.SysTenantDict;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 权限相关redis操作类
 *
 * @author zzf
 * @date 2020/12/23 9:28
 */
public class RedisServer4Role {

    /**
     * 缓存用户菜单权限信息
     * <p>
     * 形成的redis架构如下：
     * sys:__
     * .....|_dict:__SYS_DICT_CODE1__key1:DictContentBO1
     * ...........|...............|__key2:DictContentBO2
     * ...........|
     * ...........|__SYS_DICT_CODE2__key1:DictContentBO1
     * ...........|...............|__key2:DictContentBO2
     * ...........|
     * ...........|_tenant:__
     * .....................|_TENANT_ID1:__SYS_DICT_CODE1__key3:DictContentBO3
     * .....................|...........|...............|__key4:DictContentBO4
     * .....................|...........|
     * .....................|...........|__SYS_DICT_CODE2__key5:DictContentBO5
     * .....................|...........................|__key6:DictContentBO6
     * .....................|
     * .....................|_TENANT_ID2:__SYS_DICT_CODE1__key3:DictContentBO3
     * .................................|...............|__key4:DictContentBO4
     * .................................|
     * .................................|__SYS_DICT_CODE2__key5:DictContentBO5
     * .................................................|__key6:DictContentBO6
     */
    public static void setSysDict(List<SysDict> dictList) {
        DictContentBO contentBO = new DictContentBO();
        dictList.forEach(dict -> {
            DictContentList content = dict.getContent();
            Map<String, String> temp = new HashMap<>();
            content.forEach(s -> {
                contentBO.setValue(s.getValue());
                contentBO.setEnValue(s.getEnValue());
                contentBO.setFtValue(s.getFtValue());
                if (StringUtils.isEmpty(s.getKey())) {
                    System.out.println("+++++数据字典的key值为空，请检查数据字典对应的值++++++");
                }
                temp.put(s.getKey().toString(), JSON.toJSONString(contentBO));
            });
            RedisUtil.getInstance().hSet(RedisKeyHelper.sysDict(dict.getCode()), temp);
        });
    }

    /**
     * 缓存用户菜单权限信息
     */
    public static void setTenantDictList(List<SysTenantDict> dictList) {
        DictContentBO contentBO = new DictContentBO();
        dictList.forEach(dict -> {
            DictFieldFillUtils.ENABLE_CUSTOMIZED_KEY_SET.add(dict.getCode());
            DictContentList content = dict.getContent();
            Map<String, String> temp = new HashMap<>();
            content.forEach(s -> {
                contentBO.setValue(s.getValue());
                contentBO.setEnValue(s.getEnValue());
                contentBO.setFtValue(s.getFtValue());
                temp.put(s.getKey().toString(), JSON.toJSONString(contentBO));
            });
            RedisUtil.getInstance().hSet(RedisKeyHelper.sysTenantDict(dict.getSysDictId(), dict.getCode()), temp);
        });
    }


    /**
     * 缓存用户菜单权限信息
     */
    public static void setUserMenus(SysStaffAllVO staffAllVO) {
        if (staffAllVO == null) {
            return;
        }
        List<SysStaffAllVO.Menu> menuList = staffAllVO.getMenuList();
        Map<String, String> menuMap = new HashMap<>(32);
        menuList.forEach(s -> {
            menuMap.put(s.getName(), JSON.toJSONString(s.getOperations()));
        });
        RedisUtil.getInstance().hSet(RedisKeyHelper.userMenu(staffAllVO.getId()), menuMap);
    }

    /**
     * 缓存指定用户权限列表
     */
    public static void setUserRoles(Long staffId, List<String> roleList) {
        if (staffId == null) {
            return;
        }
        RedisUtil.getInstance().set(RedisKeyHelper.role(staffId), roleList.toString());
    }

    public static void clearUserCache(Long staffId) {
        //redis缓存
        AuthUtils.clearRedisCache(staffId);
        //本地缓存
        AuthUtils.clearLocalCache(staffId);
    }

}
